const express = require("express");//注册路由用
var router = express.Router();
let db = require("../model/db");
let _ = require("lodash");

//作者:唐嘉诚
//添加一个导演
router.post("/:id", async (req, res) => {
    const data = await db.Movie.findById(req.params.id);
    const count = await db.Movie.countDocuments(data);
    const director = data.director;
    director.push(req.body);
    const _id = data._id;
    const updateOne = await db.Movie.updateOne({
        _id
    }, data)
    res.send({
        code: 0,
        message: "",
        count,
        data
    });
})

//作者:唐嘉诚   
//删除一个导演 
router.delete("/:id/:directorid", async (req, res) => {
    const data = await db.Movie.findById(req.params.id);
    const director = data.director;
    for (let i = 0; i < director.length; i++) {
        if (req.params.directorid == director[i]._id) {
            director.splice(i, 1);
            break;
        }
    }
    const _id = data._id;
    const updateOne = await db.Movie.updateOne({
        _id
    }, data);
    res.send({
        code: 0,
        message: "",
        count,
        data
    });
})

//作者:唐嘉诚
//添加一个演员   
router.post("/:id/actor", async (req, res) => {
    const data = await db.Movie.findById(req.params.id);
    const count = await db.Movie.countDocuments(data);
    const actor = data.actor;
    actor.push(req.body);
    const _id = data._id;
    const updateOne = await db.Movie.updateOne({
        _id
    }, data)
    res.send({
        code: 0,
        message: "",
        count,
        data
    });
})

//作者:唐嘉诚
//删除一个演员 
router.delete("/:id/:actorid/actor", async (req, res) => {
    const data = await db.Movie.findById(req.params.id);
    const actor = data.actor;
    for (let i = 0; i < actor.length; i++) {
        if (req.params.actorid == actor[i]._id) {
            actor.splice(i, 1);
            break;
        }
    }
    const _id = data._id;
    const updateOne = await db.Movie.updateOne({
        _id
    }, data);
    res.send({
        code: 0,
        message: "",
        count,
        data
    });
})
//作者:唐嘉诚
//添加一个图片
router.post("/image/:id", async (req, res) => {
    const data = await db.Movie.findById(req.params.id);
    const count = await db.Movie.countDocuments(data);
    const images = data.images;
    images.push(req.body.images);
    const _id = data._id;
    const updateOne = await db.Movie.updateOne({
        _id
    }, data)
    res.send({
        code: 0,
        message: "",
        count,
        data
    });
})

//作者:唐嘉诚
//删除一个图片 
router.delete("/images/image/:id", async (req, res) => {
    const data = await db.Movie.findById(req.params.id);
    let images = data.images;
    images = images.filter(item => item !== req.query.deleteUrl);
    const updateOne = await db.Movie.updateOne({ _id: req.params.id }, {
        images
    });
    res.send({
        code: 0,
        message: "",
        count,
        data
    });
})
/**
 * lsc
 * 添加电影
 */
router.post("/", async (req, res) => {
    const data = await db.Movie.create(req.body);
    const count = await db.Movie.countDocuments(data);
    res.send({
        code: 0,//错误返回 1  
        message: "",//错误消息
        count,
        data//具体的数据  ，添加返回对象
    });
})

/**
 * lsc
 * 删除电影
 */
router.delete("/:id", async (req, res) => {
    await db.Movie.deleteOne({ _id: req.params.id });
    res.send({
        code: 0,//错误返回 1  
        message: "",//错误消息
        "count": "",
        "data": req.params.id//具体的数据  ，添加返回对象
    });

})

/**
 * lsc
 * 编辑电影
 */
router.put("/:id", async (req, res) => {
    await db.Movie.updateOne({
        _id: req.params.id//条件对象,根据id修改
    }, req.body);
    res.send({
        code: 0,//错误返回 1  
        message: "",//错误消息
        "count": "",
        "data": req.body//具体的数据  ，添加返回对象
    });

})

/**
 * lsc
 * 获取所有电影
 */
router.get("/", async (req, res) => {
    if (!req.query.field) {
        req.query.field = "_id";
    }
    if (!req.query.sort) {
        req.query.sort = "asc";
    }
    let s = req.query.field;
    if (req.query.sort === "desc") {
        s = "-" + s;
    }
    //分页
    if (!req.query.page) {
        req.query.page = 1;
    }
    if (!req.query.limit) {
        req.query.limit = 10;
    }
    const page = parseInt(req.query.page);
    const limit = parseInt(req.query.limit);
    let condition = {};
    if (req.query.key) {
        let exp = new RegExp(req.query.key, "i") //不区分大小写
        condition = {
            $or: [
                { name: { $regex: exp } },    //可根据电影名,英文名,电影类型,上映地区模糊查询
                { ename: { $regex: exp } },
                { type: { $regex: exp } },
                { area: { $regex: exp } },
            ]
        };
    }
    const data = await db.Movie.find(condition).sort(s).skip((page - 1) * limit).limit(limit);
    const count = await db.Movie.count(condition);
    res.send({
        code: 0,//错误返回 1  
        message: "",//错误消息
        count: count,//总数居量
        data//具体的数据  ，添加返回对象
    });
})
/**
 * lsc
 * 获取单个电影
 */
router.get("/:id", async (req, res) => {
    const data = await db.Movie.findById(req.params.id)
    const count = await db.Movie.countDocuments(data);
    res.send({
        code: 0,//错误返回 1  
        message: "",//错误消息
        "count": count,//总数居量
        data//具体的数据  ，添加返回对象
    });
})

module.exports = router;